﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;

namespace NewStart
{
    public partial class AppForm : Form
    {
        List<AppMenuItem> apps = new List<AppMenuItem>();
        public bool ret = false;

        public AppForm()
        {
            InitializeComponent();
            this.Top = Screen.PrimaryScreen.Bounds.Height - (Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height) - this.Height;
            this.MouseWheel += AppForm_MouseWheel;

            ReloadMenu();
        }

        public Color back
        {
            set { this.BackColor = value; textBox1.BackColor = value; }
        }

        public void ReloadMenu()
        {
            foreach (string shortcut in ShortcutsHelper.ListShortcuts(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu)))
            {
                try
                {
                    if (ShortcutsHelper.GetShortcutDestination(shortcut) != "")
                        AddApplication(ShortcutsHelper.GetShortcutName(shortcut), ShortcutsHelper.GetShortcutDestination(shortcut));
                }
                catch { }
            }

            flowLayoutPanel1.SuspendLayout();
        }

        private void mouseKeyEventProvider1_MouseClick(object sender, MouseEventArgs e)
        {
            if (PointsHelper.OutMenu(e.Location, this))
            {
                this.Invoke(new ThreadStart(Close));
            }
        }

        void AddApplication(string name, string path)
        {
            AppMenuItem a = new AppMenuItem(path, name);
            flowLayoutPanel1.Controls.Add(a);
            apps.Add(a);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
                label3.Visible = true;
            else
                label3.Visible = false;

            flowLayoutPanel1.ResumeLayout();
            flowLayoutPanel1.Controls.Clear();
            foreach (AppMenuItem app in apps)
            {
                if (app.name.ToLower().Contains(textBox1.Text))
                {
                    flowLayoutPanel1.Controls.Add(app);
                }
            }
            flowLayoutPanel1.SuspendLayout();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void AppForm_Load(object sender, EventArgs e)
        {
            MouseHelper.MouseClick += mouseKeyEventProvider1_MouseClick;
        }

        private void AppForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            MouseHelper.MouseClick -= mouseKeyEventProvider1_MouseClick;
            MouseHelper.Enabled = false;
        }

        void AppForm_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta >= 1)
            {
                AnimationHelper.MoveApps(flowLayoutPanel1, 50, false);
                //foreach (Control control in flowLayoutPanel1.Controls)
                //    control.Top += 50;
            }

            if (e.Delta <= -1)
            {
                AnimationHelper.MoveApps(flowLayoutPanel1, -50, false);
                //foreach (Control control in flowLayoutPanel1.Controls)
                //    control.Top -= 50;
            }
        }

        private void AppForm_Shown(object sender, EventArgs e)
        {
            MouseHelper.Enabled = true;
            textBox1.Focus();
        }

        private void label2_Click(object sender, EventArgs e)
        {
            this.ret = true;
            this.Close();
        }
    }
}
